package com.appspot.yozmcup.DAO;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import oauth.signpost.OAuthConsumer;

import com.appspot.yozmcup.api.Request;
import com.google.appengine.repackaged.org.json.JSONObject;

public class YozmDAO {
	public static boolean isYozmMember(OAuthConsumer consumer){
		try {
			return new JSONObject(new Request(consumer).getResult("https://apis.daum.net/yozm/v1_0/user/joined.json")).getBoolean("joined");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static String getAvailableNickname(String nickname){
		try {
			return new Request().getResult("https://apis.daum.net/yozm/v1_0/user/validate_nickname.json?nickname="+URLEncoder.encode(nickname,"UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static JSONObject joinYozmObject(OAuthConsumer consumer, String nickname, String sex, String birthday, String bloodType){
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(getParamType("nickname", nickname));
			sb.append(getParamType("&sex", sex));
			sb.append(getParamType("&birthday", birthday));
			sb.append(getParamType("&bloodType", bloodType));
			return new JSONObject(new Request(consumer).getResult("https://apis.daum.net/yozm/v1_0/user/join.json?"+sb.toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String joinYozm(OAuthConsumer consumer, String nickname, String sex, String birthday, String bloodType){
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(getParamType("nickname", nickname));
			sb.append(getParamType("&sex", sex));
			sb.append(getParamType("&birthday", birthday));
			sb.append(getParamType("&bloodType", bloodType));
			return new Request(consumer).getResult("https://apis.daum.net/yozm/v1_0/user/join.json?"+sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getMember(OAuthConsumer consumer, String command, String url_name){
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(getParamType("url_name", url_name));
			sb.append(getParamType("&count", "100"));
			return new Request(consumer).getResult("https://apis.daum.net/yozm/v1_0/user/"+ command + ".json?"+sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getMember(OAuthConsumer consumer, String command){
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(getParamType("count", "100"));
			return new Request(consumer).getResult("https://apis.daum.net/yozm/v1_0/user/"+ command + ".json?"+sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static JSONObject getMyInfoObject(OAuthConsumer consumer){
		try {
			return new JSONObject(new Request(consumer).getResult("http://apis.daum.net/yozm/v1_0/user/show.json"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getMyInfo(OAuthConsumer consumer){
		try {
			return new Request(consumer).getResult("http://apis.daum.net/yozm/v1_0/user/show.json");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static JSONObject getWrite(OAuthConsumer consumer, String message, String img_url){
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(getParamType("message", message));
			sb.append(getParamType("&img_url", img_url));
			return new JSONObject(new Request(consumer).getResult("https://apis.daum.net/yozm/v1_0/message/add.json?"+sb.toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getParamType(String name, String value) throws UnsupportedEncodingException{
		return name + "=" + URLEncoder.encode(value,"UTF-8");
	}
}
